package com.weijing.pretty_app.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weijing.pretty_app.R
import com.weijing.pretty_app.adapter.NenFuAdapter
import com.weijing.pretty_app.databinding.ActivityNenFuNextBinding
import com.weijing.pretty_app.models.SelectModel
import com.weijing.pretty_app.util.BluetoothSocketManager
import com.weijing.pretty_app.util.HexConverter
import com.weijing.pretty_app.util.KeyUtil

private const val TAG = "NenFuNextActivity"

/**
 * 嫩肤选择部门页面
 * shao_g 2025年4月14日21:37:15
 */
class NenFuNextActivity : BaseActivity() {

    private val binding by lazy { ActivityNenFuNextBinding.inflate(layoutInflater) }

    override fun getMainView(): View = binding.main

    //方向L、R
    private var commandDirection = "01"
    private var buWeiCommand = "01"
    private var fangXiang = "L"
    private var fangXiangL = "L"
    private var fangXiangR = "R"
    private var locationL = 0 //默认脸部 0开始，0-5
    private var locationR = 0 //默认脸部 0开始，0-5
    private var mSelectMode: SelectModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        initClick()
        initAdapter()
    }

    private fun initClick() {
        binding.nenFuNextIvBack.setOnClickListener { finish() }

        binding.nenFuNextIvNext.setOnClickListener {
            val maps = mutableMapOf<Int, SelectModel>()
            maps[0] = SelectModel(
                model1 = mSelectMode?.model1, direction = fangXiangL, sex = -1, location = locationL
            )
            maps[1] = SelectModel(
                model1 = mSelectMode?.model1, direction = fangXiangR, sex = -1, location = locationR
            )
            sendData()
            LiveEventBus.get<Map<Int, SelectModel>>(KeyUtil.TO_NENFU_HOME_ACTIVITY).post(maps)
            startActivity(Intent(this@NenFuNextActivity, NenFuHomeActivity::class.java))
//            finish()
        }

        binding.nenFuNextClL.setOnClickListener {
            fangXiang = "L"
            setActiveButton(true)
            commandDirection = "01"
            binding.nenFuNextIvOrderL.visibility = View.VISIBLE
            binding.nenFuNextIvOrderR.visibility = View.INVISIBLE
        }

        binding.nenFuNextClR.setOnClickListener {
            fangXiang = "R"
            setActiveButton(false)
            commandDirection = "02"
            binding.nenFuNextIvOrderL.visibility = View.INVISIBLE
            binding.nenFuNextIvOrderR.visibility = View.VISIBLE
        }

        LiveEventBus.get<SelectModel>(KeyUtil.TO_NenFuNextActivity).observeSticky(this, {
            mSelectMode = it
        })
    }

    private fun setActiveButton(isLeftActive: Boolean) {
        if (isLeftActive) {
            binding.nenFuNextClL.background =
                ContextCompat.getDrawable(this, R.drawable.shape_white_top30)
            binding.nenFuNextClR.background =
                ContextCompat.getDrawable(this, R.drawable.shape_yellow_bottom30)

            binding.nenFuNextIvL.setImageResource(R.mipmap.icon_sb_yellow)
            binding.nenFuNextIvR.setImageResource(R.mipmap.icon_sb_black)

            binding.nenFuNextTvL.setBackgroundResource(R.drawable.shape_circle_yellow)
            binding.nenFuNextTvR.setBackgroundResource(R.drawable.shape_circle_041332)

            binding.nenFuNextTvL.setTextColor(ContextCompat.getColor(this, R.color.color_FFC300))
            binding.nenFuNextTvR.setTextColor(ContextCompat.getColor(this, R.color.black))
        } else {
            binding.nenFuNextClR.background =
                ContextCompat.getDrawable(this, R.drawable.shape_white_bottom30)
            binding.nenFuNextClL.background =
                ContextCompat.getDrawable(this, R.drawable.shape_yellow_top30)

            binding.nenFuNextIvR.setImageResource(R.mipmap.icon_sb_yellow)
            binding.nenFuNextIvL.setImageResource(R.mipmap.icon_sb_black)

            binding.nenFuNextTvR.setBackgroundResource(R.drawable.shape_circle_yellow)
            binding.nenFuNextTvL.setBackgroundResource(R.drawable.shape_circle_041332)
            binding.nenFuNextTvR.setTextColor(ContextCompat.getColor(this, R.color.color_FFC300))
            binding.nenFuNextTvL.setTextColor(ContextCompat.getColor(this, R.color.black))
        }

        binding.nenFuNextClL.requestLayout()
        binding.nenFuNextClR.requestLayout()
    }

    private fun initAdapter() {
        val adapter = NenFuAdapter(
            listOf(
                resources.getString(R.string.face), resources.getString(R.string.Arms),
                resources.getString(R.string.chest), resources.getString(R.string.back),
                resources.getString(R.string.Abdomen), resources.getString(R.string.Legs)
            )
        )

        binding.nenFuNextRV.adapter = adapter

        adapter.onClickListener = object : NenFuAdapter.OnClickListener {
            override fun onItemClickListener(position: Int, name: String) {
                when (fangXiang) {
                    "L" -> {
                        locationL = position
                    }

                    "R" -> {
                        locationR = position
                    }
                }
                when (position) {
                    0 -> {
                        buWeiCommand = "01"
                    }

                    1 -> {
                        buWeiCommand = "02"
                    }

                    2 -> {
                        buWeiCommand = "03"
                    }

                    3 -> {
                        buWeiCommand = "04"
                    }

                    4 -> {
                        buWeiCommand = "05"
                    }

                    5 -> {
                        buWeiCommand = "06"
                    }
                }

                adapter.setIndex(position)
            }
        }
    }

    private fun sendData() {
        val commentAll =
            KeyUtil.NEN_FU_COMMAND +
                    "0" + (locationL + 1) +
                    " 0" + (locationR + 1) +
                    " " +
                    KeyUtil.EE
        Log.i(TAG, "嫩肤-- ${commentAll}")
        val data = HexConverter.hexStringToByteArray(commentAll)
        BluetoothSocketManager.send(data)
    }
}